<template>
 <div>
        <div class="login_text"><h1>注册</h1><p>千峰医疗服务后台管理系统</p></div>
          <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="昵称"  prop="name">
    <el-input v-model="ruleForm.name" ></el-input>
  </el-form-item>
    <el-form-item label="身份确认" prop="region" >
    <el-select v-model="ruleForm.region" placeholder="请选择身份信息" >
      <el-option label="我是管理员" value="管理员"></el-option>
      <el-option label="我是用户" value="用户"></el-option>
    </el-select>
    </el-form-item>
  <el-form-item label="账号" prop="text">
    <el-input type="text" v-model="ruleForm.text" ></el-input>
  </el-form-item>
    <el-form-item label="密码" prop="pass">
    <el-input type="password" v-model="ruleForm.pass" ></el-input>
   



  </el-form-item>
    <el-form-item label="确认密码" prop="checkPass">
    <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">注册</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
    <el-button @click="logonFn('ruleForm')">去登录</el-button>
  </el-form-item>
</el-form>
 </div>
</template>

<script>
import { LogonFn, zhuceFn } from '../api/login';
import eventBus  from '../utils/busEvent'


export default {
 data() {
     
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入账号'));
        } else {
           var regexp = /^[a-zA-Z]\w{5,29}$/
         if (!regexp.test(value)) {
             callback(new Error('6-30位字母、数字或字母开头'));  
            } else{
  LogonFn({user:value}).then(res=>{ 
        if(res.code==200){
callback()
        }else{
 callback(new Error('该账号已被注册'));
        }
        })     
        }
            }
      };

//      
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        }  else {
                let reg1 = /\d+/;
             
 if (! reg1.test(value)) {
                callback(new Error('密码太简单了'));

            } else {
          callback();
            
            }
            



        }
      }
       var validatePass3 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      }
      return {
      
        currentView:'denglu',
        ruleForm: {
            name:'',
          text: '',
          pass: '',
          checkPass:''
        },
 
        rules: {
          text: [
            { validator: validatePass, trigger: 'blur' }
          ],
          pass: [
            { validator: validatePass2, trigger: 'blur' }
          ],checkPass: [
            { validator: validatePass3, trigger: 'blur' }
          ],
          name:[{ required: true, message: '给自己取个昵称吧', trigger: 'blur' }],
         region:[{ required: true, message: '表明身份哟', trigger: 'change' }]
        }
      };
    },
  
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {

  if(valid){
           zhuceFn(this.ruleForm).then(res=>{
     if (res.code==200) {
            alert('注册成功');
          } else {
          
            return false;
          }
})
  }
        
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      logonFn(){ 
          eventBus.$emit('currentView','denglu')
      },
        
 
    }
  }

</script>

<style scoped>
#login .login_text{ 
  text-align: center;
  margin-bottom: 20px;
  margin-left: 70px;

}
#login .login_text p{ 
    margin-top:50px
}
</style>



